package com.a01_2021.thousandstimes.saas.controller;


import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.exception.CustomExceptionType;
import com.a01_2021.thousandstimes.general.jwt.JwtTokenUtil;
import com.a01_2021.thousandstimes.saas.entity.SysInstance;
import com.a01_2021.thousandstimes.saas.service.SysInstanceService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author FRQ
 * @since 2021-02-22
 */
@RestController
@RequestMapping("/sys-instance")
public class SysInstanceController {

    @Resource
    private SysInstanceService sysInstanceService;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    /**
     * 用于添加实例
     * @param master 访问实例id
     * @param instanceName 新势力名
     * @param token 用户JWT令牌
     * @return 返还AjaxResponse
     * @throws SQLException SQL执行异常
     * @throws ClassNotFoundException 找不到驱动类异常
     */
    @PreAuthorize("hasAuthority('sys-instance-addInstance')")
    @PostMapping("/addInstance")
    @DS("#header.instanceid")
    public AjaxResponse addInstance(@RequestHeader(name = "instanceid") String master, String instanceName, @RequestHeader(name = "${jwt.header}") String token) throws SQLException, ClassNotFoundException {
        if (StringUtils.isEmpty(master) || !master.equals("master")) {
            //过滤非SAAS
            return AjaxResponse.error("实例仅允许SAAS管理平台账户创建");
        }
        if (StringUtils.isEmpty(instanceName)) {
            return AjaxResponse.error(CustomExceptionType.USER_INPUT_ERROR, "新实例名不得为空");
        }
        if (sysInstanceService.isInstance(instanceName)) {
            return AjaxResponse.error("实例名已被占用");
        }
        Map<String, String> tempUser = sysInstanceService.addInstance(instanceName, Long.parseLong(jwtTokenUtil.getUserIdFromToken(token)));
        return AjaxResponse.success(tempUser);
    }

    /**
     * 用于给实例续租
     * @param instanceId 访问实例id
     * @param renewalInstanceId 续租实例id
     * @param token JWT令牌
     * @param renewalDay 续租天数
     * @return 返还AjaxResponse
     */
    @PreAuthorize("hasAuthority('sys-instance-renewalInstance')")
    @PostMapping("/renewalInstance")
    @DS("#header.instanceid")
    public AjaxResponse renewalInstance(@RequestHeader(name = "instanceid")String instanceId,String renewalInstanceId,@RequestHeader(name = "${jwt.header}") String token,int renewalDay) throws CustomException {
        if (StringUtils.isEmpty(instanceId) || !instanceId.equals("master")) {
            //过滤非SAAS
            return AjaxResponse.error("实例仅允许SAAS管理平台账户续费");
        }
        if (StringUtils.isEmpty(renewalInstanceId))
            return AjaxResponse.error("续租实例id不可为空");
        if (renewalDay <= 0)
            return AjaxResponse.error("实例续租时间需大于0日");
        SysInstance sysInstance = sysInstanceService.renewalInstance(renewalInstanceId, renewalDay);
        return AjaxResponse.success(sysInstance);
    }

    /**
     * 通过实例id查询实例
     *
     * @param instanceId    访问实例id
     * @param getInstanceId 查询实例id
     * @return AjaxResponse
     * @throws CustomException 当实例不存在时抛出该异常
     */
    @PreAuthorize("hasAuthority('sys-instance-getInstanceInfo')")
    @DS("#header.instanceid")
    @PostMapping("getInstanceInfo")
    public AjaxResponse getInstanceInfo(@RequestHeader(name = "instanceid") String instanceId, long getInstanceId) throws CustomException {
        if (StringUtils.isEmpty(instanceId) || !instanceId.equals("master")) {
            //过滤非SAAS
            return AjaxResponse.error("该接口只允许SAAS管理账户调用");
        }
        SysInstance sysInstance = sysInstanceService.getInstanceInfo(getInstanceId);
        return AjaxResponse.success(sysInstance);
    }
}
